﻿using d1Common.WebController;
using d1Project.Modules.Business.doKKX.Domain;
using d1Project.Modules.Business.doKKX.Models;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Business.doXZ.Controllers.a1
{
    [Route("doXZ/XZ_a1_s")]
    public class XZ_a1_sController : Authorize1Controller
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private doXZDbContext db = new doXZDbContext();

        public async Task<IHttpActionResult> Get([FromUri]KKX_Sony_Hdth_GetVm model)
        {
            var entities = db.XZ_a1_s.AsQueryable();
            if (this.UserId != "admin")
            {
                entities = entities.Where(x => x.UserId == this.UserId);
            }
            var result = await entities.Select(t => new
            {
                t.Id,
                t.Name,
                t.Wbxs,
                t.Cl,
                t.Jihuo,
                t.bezm,
                t.Wdfd,
                t.Wendu,
                t.Csn,
                t.UserId,
                t.resulf
            }).OrderByDescending(t => t.Id).ToPagedListAsync(model);
            return Ok(new { total = entities.Count(), rows = result });
        }
        public async Task<IHttpActionResult> Get(int id)
        {
            var entity = await db.XZ_a1_s.Where(t => t.Id == id).Select(t => new
            {
                t.Id,
                t.Name,
                t.Wbxs,
                t.Cl,
                t.Jihuo,
                t.bezm,
                t.Wdfd,
                t.Wendu,
                t.Csn,
                t.UserId,
                t.resulf
            }).FirstOrDefaultAsync();
            if (entity == null) this.Error("查询数据不存在");
            return Ok(entity);
        }

        public async Task<IHttpActionResult> Post(KKX_Sony_Hdth_PostVm model)
        {
            var gs = await db.XZ_a1_s.Where(x=>x.Code == "Sony_Hdth").FirstOrDefaultAsync();
            if (gs == null || string.IsNullOrEmpty(gs.Name)) this.Error("当前公式不存在");

            XZ_a1_s entity = new XZ_a1_s();
            entity.Id = Convert.ToInt32(TextHelper.GenerateRandomNumbers(8));
            entity.Name = gs.Name;
            entity.Wbxs = model.Wbxs;
            entity.Cl = model.Cl;
            entity.Jihuo = model.Jihuo;
            entity.bezm = model.bezm;
            entity.Wdfd = model.Wdfd;
            entity.Wendu = model.Wendu;
            entity.Csn = model.Csn;
            entity.UserId = this.UserId;
            entity.resulf = iec.Hdth(entity.Wbxs, entity.Cl, entity.Jihuo, entity.bezm, entity.Wdfd, entity.Wendu, entity.Csn);
            if (model.Type == 1)
            {
                db.XZ_a1_s.Add(entity);
                await db.SaveChangesAsync();
                return Ok();
            }
            else
            {
                return Ok(entity.resulf);
            }
        }
        public async Task<IHttpActionResult> Put(KKX_Sony_Hdth_PutVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            var entity = await db.XZ_a1_s.FindAsync(model.Id);
            if (entity == null) this.Error("修改数据不存在");
            entity.Wbxs = model.Wbxs;
            entity.Cl = model.Cl;
            entity.Jihuo = model.Jihuo;
            entity.bezm = model.bezm;
            entity.Wdfd = model.Wdfd;
            entity.Wendu = model.Wendu;
            entity.Csn = model.Csn;
            entity.UserId = model.UserId;
            entity.resulf = iec.Hdth(entity.Wbxs, entity.Cl, entity.Jihuo, entity.bezm, entity.Wdfd, entity.Wendu, entity.Csn);
            await db.SaveChangesAsync();
            return Ok(entity.resulf);
        }
        public async Task<IHttpActionResult> Delete(string Id)
        {
            var entity = await db.XZ_a1_s.FindAsync(Id);
            db.XZ_a1_s.Remove(entity);
            await db.SaveChangesAsync();
            return Ok();
        }
    }

    public class KKX_Sony_Hdth_GetVm : VmBase
    {
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }
    }
    public class KKX_Sony_Hdth_PostVm
    {
        /// <summary>
        /// 类型（0计算 1提交）
        /// </summary>
        public int Type { get; set; }
    }
    public class KKX_Sony_Hdth_PutVm
    {
        /// <summary>
        /// Id
        /// </summary>
        [Required]
        public int Id { get; set; }
    }
}